/*消息列表js*/
var detailData;
var msgTable;
function getMsgList() {
    if (typeof msgTable !== 'undefined') {
    	msgTable.fnPageChange('first');
        return;
    }
    msgTable = $("#businessList").DataTable({
        "sAjaxSource": _path + "/info/forinfo",     //服务端
        "fnServerParams": function (aoData) {               //ajax请求参数
            aoData.push(
                { "name": "type", "value": $("#type",$("#formQuery")).val() },
                { "name": "flag", "value": $("#flag",$("#formQuery")).val() }
            );
        },
        "fnServerData": function(sUrl, aoData, fnCallback) {
            var data = new Array();
            for(var i in aoData)
                data.push(aoData[i].name + '=' + aoData[i].value);
            $.ajax( {
                "type": "POST",
                "url": sUrl,
                "dataType": "json",
                "data": data.join('&'),
                "success": function(resp) {
                    detailData = resp.dataList;
                    fnCallback(resp);
                }
            });
        },
        "sServerMethod": "POST",    //请求方式
        "sAjaxDataProp": "dataList",//后台返回数据名
        "fnCreatedRow": function () {
            i++;
        },
        "fnPreDrawCallback": function () {
            i = 1;
        },
        "aoColumns": [
                      { "sTitle": "", "sWidth": "2%",
                          "mData": function (obj) {
                              return '<span class="row-details row-details-close" id="'+obj.id+'"></span>';
                          }
                      },
                      { "sTitle": "序号", "sWidth": "2%",
                          "mData": function () {
                              return i;
                          }
                      },
                      { "sTitle": "标题", "mData": "title", "sWidth": "15%" },
                      { "sTitle": "类型", "mData": "type", "sWidth": "15%" ,"mRender": function (data, type, full) {
                          var typename = '';
                          switch (data) {
                              case '4':
                            	  typename = "会员消息";
                                  break;
                              case '2':
                            	  typename = "管理员发送";
                                  break;
                              default:
                            	  typename = "系统消息";
                                  break;
                          }
                          return typename;
                      }},
                      { "sTitle": "接收人", "mData": "username", "sWidth": "10%" },
                      { "sTitle": "发送人", "mData": "sendUsername", "sWidth": "10%" },
                      { "sTitle": "时间", "mData": "sendtime", "sWidth": "10%" },
                      { "sTitle": "状态", "mData": "status", "sWidth": "10%",
                          "mRender": function (data, type, full) {
                              var status = '';
                              switch (data) {
                                  case '1':
                                      status = "已读";
                                      break;
                                  case '0':
                                      status = "未读";
                                      break;
                                  default:
                                      break;
                              }
                              return status;
                          }},
			            { "sTitle": "操作", "sWidth": "10%",
			                "mData": function (data) {
			                    var status = '';
			                        status = '&nbsp;<a href="javascript:del(\'' + data.id + '\')" class="btn default btn-xs red"><i class="fa fa-trash-o" title="删除"></i></a>';
			                       if(data.type==4)
			                        status += '<a href="javascript:modalXX(\'' + data.id + '\',\'' + data.sendUserId + '\')" class="btn default btn-xs green"><i class="fa fa-envelope-o" title="发送消息"></i></a>'
			                    return status;
			                }
			            }
        ]
    });
    $('#businessList').on('click', ' tbody td .row-details', function () {
        var nTr = $(this).parents('tr')[0];
        if ( msgTable.fnIsOpen(nTr) ) {
            /* This row is already open - close it */
            $(this).addClass("row-details-close").removeClass("row-details-open");
            $(this).parent("td").next().children("input").click();
            msgTable.fnClose( nTr );
        }else {
            /* Open this row */
            $(this).addClass("row-details-open").removeClass("row-details-close");
            $(this).parent("td").next().children("input").click();
            msgTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
        }
    });
}

function oper(type) {}

function modalXX(msgid,sendUserId) {
    $('#panel').on('shown.bs.modal', function () {
        $("#msgid", $("#panel")).val(msgid);
        $("#userid", $("#panel")).val(sendUserId);
    }).modal('show');
}
function del(msgid){
	if (confirm("确认删除选中的记录？")){
		App.ajax(_path + "/info/del", "id="+msgid, function (data) {
			 switch (data.statusCode) {
	         case 200 :
	             App.bubble(data.statusCode, "删除成功！");
	             getMsgList();
	             break;
	         case 500 :
	             App.bubble(data.statusCode, "操作失败！");
	             break;
	         default :
	             App.bubble(500, "操作失败！");
	             break;
	     }
	     });
	} 
}
function rd() {
    var cs = "";
    switch (Math.floor(Math.random() * 5 + 1)) {
        case 1:
            cs = "yellow";
            break;
        case 2:
            cs = "blue";
            break;
        case 3:
            cs = "green";
            break;
        case 4:
            cs = "red";
            break;
        case 5:
            cs = "purple";
            break;
        default:
            cs = "yellow";
            break;
    }
    return cs;
}

function fnFormatDetails(nTr) {
    var html = new Array();
    var data = detailData[nTr._DT_RowIndex]
    html.push('<form id="subTable">');
    html.push('<div class="line_warp">');
    html.push(conver(data.context));
    html.push('</div>');
    html.push('</form>');
    return html.join('');
}